//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    defaultArray: [],
    submitCount: 50
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail);
    let formid = e.detail.formId;
    this.detailFromIds(formid)
  },
  detailFromIds: function (formId){
    let formIds=app.globalData.globalDataFormIds || [];
    let data={
      formId:formId,
      expire:parseInt(new Date().getTime()/1000)+604800
    }
    formIds.push(data);
    app.globalData.globalDataFormIds = formIds;
    this.setData({
      globalDataFormIds: JSON.stringify(formIds)
    })
  },
  mytap: function (e) {
    console.log(e.timeStamp + '- tap')
  },
  formReset: function () {
    console.log('form发生了reset事件')
  },
  onLoad: function () {
    /* if (app.globalData.userInfo) {
      this.setUserInfo(app.globalData.userInfo);
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        // this.setData({
        //   userInfo: res.userInfo,
        //   hasUserInfo: true
        // })
        // console.log(res.userInfo);
        this.setUserInfo(res.userInfo);
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo;
          this.setUserInfo(res.userInfo);
        }
      })
    }
    this.setArray(); */
    
  },
  setUserInfo:function(userInfo){
    this.setData({
      userInfo: userInfo,
      hasUserInfo: true
    })
    // var globalData = app.globalData;//这里存储了appid、secret、token串  
    // var link = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + globalData.appid + '&secret=' + globalData.appsecret + '&js_code=' + userInfo.code + '&grant_type=authorization_code';
    // wx.request({
    //   url: link,
    //   data: {},
    //   method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT  
    //   // header: {}, // 设置请求的 header  
    //   success: function (res) {
    //     var obj = {};
    //     obj.openid = res.data.openid;
    //     obj.expires_in = Date.now() + res.data.expires_in;
    //     console.log(obj);
    //     wx.setStorageSync('user', obj);//存储openid  
    //   }
    // });
  },
  setArray:function(){
    var json = [];
    //console.log(this.data);
    for (var i = 0; i < this.data.submitCount; i++) {
      json.push({ "index": i });
    }
    this.setData({
      defaultArray: json
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
